package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.TransportationMgrRemote;
import it.polimi.travelDream.ejb.interfaces.PackageMgrRemote;
import it.polimi.travelDream.entities.Package;
import it.polimi.travelDream.entities.Transportation;

import java.io.IOException;
import java.util.Map;
import java.lang.Object;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TravelServlet
 */
@WebServlet("/AddPackageTravels")
public class AddPackageTravels extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	PackageMgrRemote pmgr;
	
	@EJB
	TransportationMgrRemote tmgr;
	
	public AddPackageTravels() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> mappa = request.getParameterMap();
		
		for (int i = 0; i < 20; i++){
			System.out.println(i);
			if (mappa.get("idTransportation"+i) != null){
				if (mappa.get("idTransportation"+i)[0] != null){
					Transportation transportation = new Transportation();
					String idTransportation = mappa.get("idTransportation"+i)[0];
					int z = Integer.parseInt(idTransportation);
					transportation = tmgr.getTransportation(z);
					pmgr.addTransportation(transportation,Integer.parseInt( mappa.get("idPackage")[0]));
				}
			}
		}
		
		
		
		request.setAttribute("message","Package's travels succesfully added. Now insert packages's hotels.");
		request.setAttribute("idPack", mappa.get("idPackage")[0]);
		request.getRequestDispatcher("/PCreatePackage3.jsp").forward(request, response); 
		//response.getOutputStream().println("<h1>TravelDream</h1><h2>" + mappa.get("name")[0]+ " succesfully registered!</h2>" + "| <a href=\"Index.jsp\"> Login </a> | Registration |");
		
	}

}
